package com.sdtv.basemodule.rx.errorhandler;

import android.content.Context;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.rx.errorhandler
 * @ClassName: RxErrorHandler
 * @Description: Rx请求错误统一处理
 * @Author: 小郭同学
 * @CreateDate: 2019-06-24 14:49
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class RxErrorHandler {

    private static final String TAG = "RxErrorHandler";

    // 错误类创建工厂
    private ErrorHandlerFactory errorHandlerFactory;
    // 错误监听
    private ResponseErrorListener errorListener;

    /**
     * 构造器
     *
     * @param builder
     */
    public RxErrorHandler(Builder builder) {
        this.errorHandlerFactory = builder.handlerFactory;
        this.errorListener = builder.errorListener;
    }


    public ErrorHandlerFactory getErrorHandlerFactory() {
        return errorHandlerFactory;
    }

    public void setErrorHandlerFactory(ErrorHandlerFactory errorHandlerFactory) {
        this.errorHandlerFactory = errorHandlerFactory;
    }


    /**
     * 创建builder工具
     *
     * @return
     */
    public static Builder builder() {

        return new Builder();

    }

    /**
     * builder构建类
     * 该类里面配置错误生产工厂 以及 错误监听
     */
    public static final class Builder {

        private ErrorHandlerFactory handlerFactory;

        private ResponseErrorListener errorListener;

        private Context context;


        public Builder() {

        }

        public Builder with(Context context) {
            this.context = context;
            return this;
        }

        public Builder setErrorListener(ResponseErrorListener errorListener) {
            this.errorListener = errorListener;
            return this;
        }


        public RxErrorHandler build() {

            if (null == context) {
                throw new IllegalStateException("Context is required");
            }

            if (null == errorListener) {
                throw new IllegalStateException("ResponseErrorListener is required");
            }

            this.handlerFactory = new ErrorHandlerFactory(context, errorListener);

            return new RxErrorHandler(this);
        }

    }


}
